﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
using System.Text;
using Kaskelot.SP.Model.Client;
using Stockholm.SOA.Kaskelot.WCF.DataContracts;
using Kaskelot.SP.Services.Client.Mapping;
using Microsoft.SharePoint;
using Kaskelot.SP.Model.Client.Forms;
using Kaskelot.SP.Core;
using Stockholm.SOA.Kaskelot.WCF.DataContracts.Analys;

namespace Kaskelot.SP.Services.Client
{
	[ServiceContract(Namespace = "http://Kaskelot.SP.Services", Name = "ClientAnalysService", ConfigurationName = "ClientAnalysService")]
	[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
	public class ClientAnalysService
	{
		[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
		[ServiceKnownType(typeof(FieldFilter))]
		[ServiceKnownType(typeof(FilterGroup))]
		[ServiceKnownType(typeof(IFilterPart))]
		[OperationContract]
		public QueryResult ExecuteQuery(Query query)
		{
			try
			{
				using (AnalysService serv = new AnalysService())
				{
					Guid valId = ApplicationContext.Current.Val.Id;

					return serv.ExecuteQuery(query, valId);
				}
			}
			catch (Exception ex)
			{
				Logger.LogError(this, LayerNames.Services, "ExecuteQuery", ex.ToString(), ex);
				return null;
			}
		}

		[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
		[OperationContract]
		public List<Entity> GetEntities()
		{
			try
			{
				List<Entity> entities = new List<Entity>();
				using (AnalysService serv = new AnalysService())
				{
					entities = serv.GetEntities();
				}

				return entities;
			}
			catch (Exception ex)
			{
				Logger.LogError(this, LayerNames.Services, "GetEntities", ex.ToString(), ex);
				throw new Exception("Could not load entities");
			}
		}

		[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
		[OperationContract]
		public List<LookupField> GetLookupInformation()
		{
			try
			{
				using (AnalysService serv = new AnalysService())
				{
					return serv.GetLookupInformation();
				}
			}
			catch (Exception ex)
			{
				Logger.LogError(this, LayerNames.Services, "GetLookupInformation", ex.ToString(), ex);
				throw new Exception("Could not load lookup information");
			}
		}

		[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
		[ServiceKnownType(typeof(FieldFilter))]
		[ServiceKnownType(typeof(FilterGroup))]
		[ServiceKnownType(typeof(IFilterPart))]
		[OperationContract]
		public List<Query> GetPersonalQueries()
		{
			try
			{
				using (AnalysService serv = new AnalysService())
				{
					return serv.GetQueriesByPerson(SPContext.Current.Web.CurrentUser.Name);
				}
			}
			catch (Exception ex)
			{
				Logger.LogError(this, LayerNames.Services, "GetPersonalQueries", ex.ToString(), ex);
				throw new Exception("Could not load queries");
			}
		}

		[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
		[ServiceKnownType(typeof(FieldFilter))]
		[ServiceKnownType(typeof(FilterGroup))]
		[ServiceKnownType(typeof(IFilterPart))]
		[OperationContract]
		public void SavePersonalQuery(Query query)
		{
			try
			{
				using (AnalysService serv = new AnalysService())
				{
					serv.SaveQuery(query, SPContext.Current.Web.CurrentUser.Name);
				}
			}
			catch (Exception ex)
			{
				Logger.LogError(this, LayerNames.Services, "SavePersonalQuery", ex.ToString(), ex);
				throw new Exception("Could not load queries");
			}
		}

		[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
		[ServiceKnownType(typeof(FieldFilter))]
		[ServiceKnownType(typeof(FilterGroup))]
		[ServiceKnownType(typeof(IFilterPart))]
		[OperationContract]
		public void SaveGlobalQuery(Query query)
		{
			try
			{
				using (AnalysService serv = new AnalysService())
				{
					serv.SaveQuery(query, null);
				}
			}
			catch (Exception ex)
			{
				Logger.LogError(this, LayerNames.Services, "SaveGlobalQuery", ex.ToString(), ex);
				throw new Exception("Could not load queries");
			}
		}

		[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
		[OperationContract]
		public void DeleteQuery(int queryId)
		{
			try
			{
				using (AnalysService serv = new AnalysService())
				{
					serv.DeleteQuery(queryId);
				}

			}
			catch (Exception ex)
			{
				Logger.LogError(this, LayerNames.Services, "DeleteQuery", ex.ToString(), ex);
				throw new Exception("Could not delete query");
			}
		}

		//[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
		//[OperationContract]
		//public string GetAnalyseInfoLinkUrl()
		//{
		//    try
		//    {
		//        using (AnalysService serv = new AnalysService())
		//        {
		//            return serv.GetAnalyseInfoLinkUrl();
		//        }
		//    }
		//    catch (Exception ex)
		//    {
		//        Logger.LogError("ClientAnalayseService.GetAnalyseInfoLinkUrl: Error:" + ex.ToString());
		//        throw new Exception("Could not get info link url");
		//    }
		//}
	}
}
